/*
给你一个未排序的整数数组 nums ，请你找出其中没有出现的最小的正整数。

请你实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案。
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution1 {
public:
    int firstMissingPositive(vector<int>& nums) {
        int n = nums.size();
        //去除负数影响
        for (int& num: nums) {
            if (num <= 0) {
                num = n + 1;
            }
        }
        for (int i = 0; i < n; ++i) {
            int num = abs(nums[i]);
            if (num <= n) {
                nums[num - 1] = -abs(nums[num - 1]);
            }
        }
        for (int i = 0; i < n; ++i) {
            if (nums[i] > 0) {
                return i + 1;
            }
        }
        return n + 1;
    }
};
class Solution2 {
public:
    int firstMissingPositive(vector<int>& nums) {
        int n = nums.size();
        for (int i = 0; i < n; ++i) {
            while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
                swap(nums[nums[i] - 1], nums[i]);
            }
        }
        for (int i = 0; i < n; ++i) {
            if (nums[i] != i + 1) {
                return i + 1;
            }
        }
        return n + 1;
    }
};
int main() {
    vector<int> n={-31,2,4,4,3,1,5,8,77,54,6};
    Solution1 solution1;
    Solution2 solution2;
    cout << solution1.firstMissingPositive(n) << endl;
    cout << solution2.firstMissingPositive(n) << endl;
    return 0;
}